package com.tablepet.activities.preview;

import android.content.SharedPreferences;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.esotericsoftware.spine.AnimationState;
import com.esotericsoftware.spine.AnimationStateData;
import com.esotericsoftware.spine.Event;
import com.esotericsoftware.spine.Skeleton;
import com.esotericsoftware.spine.SkeletonData;
import com.esotericsoftware.spine.SkeletonJson;
import com.esotericsoftware.spine.SkeletonRenderer;
import com.tablepet.activities.App;

public class Render extends ApplicationAdapter {
	OrthographicCamera camera;
	PolygonSpriteBatch batch;
	SkeletonRenderer renderer;
	TextureAtlas atlas;
	Skeleton skeleton;
	AnimationState state;
	private String petDir;

	public void create () {
		int screenWidth = App.sp.getInt("screenWidth", 0);
		petDir = App.sp.getString("ChoosedPet", "null");
		float nowScare = 0;
		float posY = 0;
		float posX = 0;
		if (App.sp.getBoolean("isChoosed",false)){
//			nowScare=0.3f;
//			posX =(screenWidth /2)-20;
//			posY =(screenWidth /2)-100;

			nowScare =0.5f;
			posX =(screenWidth /2)-20;
			posY = screenWidth /2-200;

		}else {
			petDir =App.sp.getString("PreviePet","");
			nowScare =0.5f;
			posX =(screenWidth /2)-20;
			posY = screenWidth /2-200;
		}
		if (!petDir.equals("null")&&!petDir.equals("")){
			camera = new OrthographicCamera();
			batch = new PolygonSpriteBatch();
			renderer = new SkeletonRenderer();
			renderer.setPremultipliedAlpha(false);
			atlas = new TextureAtlas(Gdx.files.internal(petDir +"/axie.atlas"));
			SkeletonJson json = new SkeletonJson(atlas);
			json.setScale(nowScare);
			SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal(petDir +"/axie.json"));
			skeleton = new Skeleton(skeletonData);
			skeleton.setPosition(posX, posY);
			AnimationStateData stateData = new AnimationStateData(skeletonData);
			state = new AnimationState(stateData);
			state.setAnimation(0, "action/appear", false);
			state.addListener(new AnimationState.AnimationStateListener() {
				@Override
				public void start(AnimationState.TrackEntry entry) {
				}
				@Override
				public void interrupt(AnimationState.TrackEntry entry) {
				}
				@Override
				public void end(AnimationState.TrackEntry entry) {
				}
				@Override
				public void dispose(AnimationState.TrackEntry entry) {
				}
				@Override
				public void complete(AnimationState.TrackEntry entry) {
					String animationName = entry.getAnimation().getName();
					if (animationName.equals("action/appear")||animationName.equals("activity/eat-chew")||animationName.equals("action/victory-pose-back-flip")){
						state.setAnimation(0, "action/idle", true);
					}
					if (animationName.equals("activity/eat-bite")){
						state.setAnimation(0, "activity/eat-chew", false);
					}
				}
				@Override
				public void event(AnimationState.TrackEntry entry, Event event) {
				}
			});
		}

		App.configChange=false;
		SharedPreferences.Editor edit = App.sp.edit();
		edit.putBoolean("isChoosed",true);
		edit.apply();
	}
	public void render () {
		if (App.configChange){
			create();
		}else {
			if (!petDir.equals("null")&&!petDir.equals("")) {
				state.update(Gdx.graphics.getDeltaTime());
				Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
				if (state.apply(skeleton)) skeleton.updateWorldTransform();
				camera.update();
				batch.begin();
				renderer.draw(batch, skeleton);
				batch.end();
			}
		}
	}
	public void resize (int width, int height) {
		if (!petDir.equals("null")&&!petDir.equals("")) {camera.setToOrtho(false);}
	}
	public void dispose () {
		if (!petDir.equals("null")&&!petDir.equals("")) {atlas.dispose();}
	}
}